package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysAttachmentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.util.Map;

@RestController
@Slf4j
public class UploadController {

    @Resource(name = "sysAttachmentService")
    private SysAttachmentService sysAttachmentService;

    /**
     * 上传图片
     * @return
     * @throws IframeException
     */
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IframeException {
        String filePath = sysAttachmentService.saveSysAttachment(file, true);
        return Result.ok(filePath);
    }

    @RequestMapping("/upload/logo")
    public Result<String> logo(StandardMultipartHttpServletRequest request) throws IframeException {
        String filePath = null;
        Map<String, MultipartFile> fileMap = request.getFileMap();
        for (String fileName : fileMap.keySet()) {
            filePath = sysAttachmentService.saveSysAttachment(fileMap.get(fileName) ,false);
        }
        return Result.ok(filePath);
    }
}
